import Base64 from "base-64";
import {generalSave,generalQueryByTableName,getMemberPoint} from '@/api/generalBusiness.js'
import {restartWeChatPay,rewriteReceipts,cancelRewriteReceiptsByCollectionSerialNumber} from '@/api/salerOrder.js'
import {weChatPay} from '@/static/js/GeneralUtil.js'
/**
 * 通用方法
 */
const mixins = {
	methods:{
		async handlesubmit(Enum,item){
			const that = this;
			if(Enum=='remove'){
				uni.showModal({
					title: "提示",
					content: "是否删除该订单记录",
					success: (data)=> {
						if (data.confirm) {
							this.generalQueryByTableName({
								TableName:'business_sales_order',
								Condition:"(Row_Status='06' OR Row_Status='16') AND Sales_Order_SerialNumber='"+item.Sales_Order_SerialNumber+"'"
							},async (business_sales_order)=>{
								let Row_Status = business_sales_order[0].Row_Status.charAt(0);
								business_sales_order[0].Row_Status= Row_Status + '7';
								const res = await generalSave({
									Data:JSON.stringify({Modify:{business_sales_order}})
								})
								if(res.Status == 'success'){
									uni.showToast({
										title:'删除成功',
										icon:'none'
									})
									this.getData?this.getData(this.form):null;
								}else{
									uni.showToast({
										title:'系统繁忙',
										icon:'none'
									})
								}
							})
						}
					},
				});
			} else if (Enum=='pay'){
				//判断该付款是否存在现金付款
				if(item.Children.sales_order_detail.some(i=>Number(i.Reality_Amount)>0)){
					const res = await restartWeChatPay({
						Collection_SerialNumber:item.Collection_SerialNumber,
						OpenId:this.Open_ID
					})
					weChatPay(res,()=>{
						uni.showToast({
							title: '支付成功!',
							icon: 'success',
							success() {
								//复写订单状态为8待修改
								that.generalQueryByTableName({
									TableName:'business_collection',
									Condition: "Row_Status = '0' AND Collection_SerialNumber='"+item.Colletion_SerialNumber+"'"
								},(data)=>{
									if(data.length==1){
										data[0].Row_Status ='8';
										that.generalSave({
											Data:JSON.stringify({Modify:{business_collection:data}})
										},()=>{
											this.getData?this.getData(this.form):null;
										})
									}
								},()=>{//失败
									uni.showToast({
										title: '已取消支付！',
										icon: 'error'
									})
								},()=>{//成功失败都会调
									this.loading = false
									uni.hideLoading();
								});
							}
						})
					})
				} else {
					let Colletion_SerialNumber = item.Colletion_SerialNumber
					uni.showModal({
						title: "确认",
						content: "是否使用积分进行支付",
						success:(data)=> {
							if (data.confirm) {
								uni.showLoading({
									title: "支付中...",
								});
								this.rewriteReceipts({Colletion_SerialNumber:item.Collection_SerialNumber},(res)=>{
									if(res.Status=="success"){
										uni.showToast({
											title:'支付成功',
											icon:'none'
										})
										this.getData?this.getData(this.form):null;
									}else{
										uni.showToast({
											title:'系统繁忙',
											icon:'none'
										})
									}
								});
							} else if (data.cancel) {
								uni.showToast({
									title:'已取消支付！',
									icon:'none'
								})
							}
						},
					})
				}
			} else if(Enum=='cancel'){
				uni.showModal({
					title: "确认",
					content: "是否要取消该订单",
					success:async (data)=> {
						if (data.confirm) {
							const res = await cancelRewriteReceiptsByCollectionSerialNumber({
								CollectionSerialNumber:item.Collection_SerialNumber
							})
							if(res.Status=="success"){
								uni.showToast({
									title:'取消成功',
									icon:'none'
								})
								this.getData?this.getData(this.form):null;
							}else{
								uni.showToast({
									title:'系统繁忙',
									icon:'none'
								})
							}
						}
					},
				})
			} else if (Enum=='receipt'){
				uni.showModal({
					title: "确认",
					content: "是否将该订单更新为已收货",
					success:async (data)=> {
						if (data.confirm) {
							this.generalQueryByTableName({
								TableName:'business_sales_order',
								Condition: "Sales_Order_SerialNumber='"+item.Sales_Order_SerialNumber+"'"
							},async (data)=>{
								if(data.length==1){
									data[0].Row_Status = data[0].Row_Status.charAt(0) + '5';
									const res = await generalSave({
										Data:JSON.stringify({Modify:{business_sales_order:data}})
									})
									if(res.Status == 'success'){
										this.getData?this.getData(this.form):null;
									}
								}
							},()=>{//失败
								uni.showToast({
									title: '系统繁忙！',
									icon: 'none'
								})
							},()=>{//成功失败都会调
								this.loading = false
								uni.hideLoading();
							});
						}
					},
				})
			} else if (Enum=='buy'){
				
				
				
			} else if (Enum=='cart'){
				
				
				
			} else {
				// this.getMemberPoint({
				// 	Data:JSON.stringify([{
				// 		tableName:'business_collection',
				// 		Condition: Base64.encode("Row_Status='0' and Collection_SerialNumber='"+item.Collection_SerialNumber+"'")
				// 	},{
				// 		tableName:'business_sales_order',
				// 		Condition: Base64.encode("Row_Status='1' and Sales_Order_SerialNumber='"+item.Sales_Order_SerialNumber+"'")
				// 	}])
				// },(res)=>{
				// 	console.log(res);
				// })
			}
		},
		async generalQueryByTableName(Data,callback){
			callback(await generalQueryByTableName(Data))
		},
		async getMemberPoint(Data,callback){
			callback(await getMemberPoint(Data))
		},
		async rewriteReceipts(Data,callback){
			callback(await rewriteReceipts(Data))
		}
	}
}

module.exports = {
	mixins
}